﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Data.PLinq.Helpers;
using DevExpress.Web.ASPxEditors;
using TheHeGeoCommercial.Dao;

namespace TheHeGeoCommercial.Commercial
{
    public partial class UcProductDetail : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                if (!IsPostBack)
                {
                    LoadPictures(Request.QueryString["id"]);
                    LoadGrid(Request.QueryString["id"]);
                    LoadHyperLink(Request.QueryString["id"]);
                    LoadGrvVideo(Request.QueryString["id"]);
                }
            }
            
        }

        protected void LoadPictures(string pId)
        {
            using (var ent = new Dao.TheheGeoCommercialEntities())
            {
                var obj = ent.SanPhams.FirstOrDefault(p => p.MaSanPham == pId);
                if (obj != null)
                {
                    var newPath = Server.MapPath(obj.ImageDirectory);
                    ASPxImageSlider1.ImageSourceFolder = obj.ImageDirectory;
                    if (!Directory.Exists(newPath))
                    {
                        Directory.CreateDirectory(newPath);
                    }
                    ASPxImageSlider1.DataBind();
                    txtDetail.Text = (string.IsNullOrEmpty(obj.ChuThich) ? "Không có dữ liệu" : obj.ChuThich);
                }
            }
        }
        private void LoadGrvVideo(string maSanPham)
        {
            using (var ent = new Dao.TheheGeoCommercialEntities())
            {
                var detail = (from d in ent.ProductVideos
                              where d.MaSanPham == maSanPham
                              select new
                              {
                                  d.Id,
                                  d.VideoLink,
                                  d.VideoName

                              }).ToList();
                grvVideo.DataSource = detail;
                grvVideo.DataBind();
            }
        }

        protected void LoadHyperLink(string pId)
        {
            using (var ent = new Dao.TheheGeoCommercialEntities())
            {
                var sp = ent.SanPhams.FirstOrDefault(p => p.MaSanPham == pId);
                if (sp != null)
                {
                    string maLoai = sp.MaLoaiSanPham;
                    var obj = (from s in ent.SanPhams
                        join l in ent.LoaiSanPhams on s.MaLoaiSanPham equals l.MaLoaiSanPham
                        where s.MaLoaiSanPham == maLoai && s.MaSanPham != pId
                        select s).Take(11).ToList();
                    int count = obj.Count;

                    int i = 1;
                    foreach (var sanPham in obj)
                    {
                        string name = "hbl" + i;
                        i++;
                        foreach (Control c in Controls)
                        {
                            var a = c as ASPxHyperLink;
                            if (a != null && a.ID.Equals(name))
                            {
                                a.NavigateUrl = "ProductDetail.aspx?id=" + sanPham.MaSanPham;
                                a.Text = sanPham.TenSanPham;
                                break;
                            }}
                    }
                    var name2 = "hbl" + i;
                    foreach (Control c in Controls)
                    {
                        var a = c as ASPxHyperLink;
                        if (a != null && a.ID.Equals(name2))
                        {
                            a.NavigateUrl = "Products.aspx?type=" + maLoai;
                            a.Text = "Xem thêm...";
                            break;
                        }
                    }
                }
               
            }
        }

        protected void LoadGrid(string pId)
        {
            using (var ent = new Dao.TheheGeoCommercialEntities())
            {
                var obj =  from d in ent.ChiTietSanPhams where d.MaSanPham==pId select d;
                {
                    var dtPict = new DataTable();
                    dtPict.Columns.Add("lbl");
                    dtPict.Columns.Add("txt");
                    int i = 1;
                    var txtAdd = "";
                    foreach (var v in obj)
                    {
                        var dr = dtPict.NewRow();
                        var add = "";
                        if (!string.IsNullOrEmpty(v.SpecificationNote))
                        {
                            add = "(" + i + ")";
                            txtAdd += add+v.SpecificationNote + Environment.NewLine;
                            i++;
                        }
                        dr["lbl"] = v.SpecificationName + add;
                        dr["txt"] = v.SpecificationTxt;
                        dtPict.Rows.Add(dr);
                    }

                    txtNote.Text = txtAdd;
                    ASPxGridView1.DataSource = dtPict;
                    ASPxGridView1.DataBind();
                }
            }
        }
    }
}